<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$kaarigar       = array();
$kaarigarDetail = array();
$kaarigarId     = (isset($_POST['kaarigarId'])) ? $_POST['kaarigarId'] : 0;
$kaarigarMasterId = "";
$totVadhGhat = 0;
$updatedKaarigarMstId = isset($_GET['kaarigarMasterId']) ? $_GET['kaarigarMasterId'] : 0;

if(isset($_POST['submitBtn']))
{
  if($_POST['kaarigarMasterId'] > 0)
  {
    $updateQuery ="UPDATE kaarigarmaster
                      SET kaarigarId = ".$_POST['kaarigarId'].", 
                          kaarigarDate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',
                          totalVadhGhat = ".$_POST['totalVadhGhat']."
                    WHERE kaarigarMasterId = ".$_POST['kaarigarMasterId'];

    $updateQueryRes = mysql_query($updateQuery);
    //Before Update Kaarigardetail We Delete Row : Starts
    $deleteDetaliQuery = "DELETE FROM kaarigardetail
                           WHERE kaarigarMasterId = ".$_POST['kaarigarMasterId'];
    $deleteDetaliQueryRes = mysql_query($deleteDetaliQuery);
    //Before Update Kaarigardetail We Delete Row : Ends
    
    $loopCount = 0;
    while($loopCount < count($_POST['particulars']))
    { 
      $particulars      = ($_POST['particulars'][$loopCount] != '') ? $_POST['particulars'][$loopCount] : 0;
      $credit           = ($_POST['credit'][$loopCount] != '') ? $_POST['credit'][$loopCount] : 0;
      $particular       = ($_POST['particular'][$loopCount] != '') ? $_POST['particular'][$loopCount] : 0;
      $debit            = ($_POST['debit'][$loopCount] != '') ? $_POST['debit'][$loopCount] : 0;
      $addDebit         = ($_POST['addDebit'][$loopCount] != '') ? $_POST['addDebit'][$loopCount] : 0;
      $addCredit        = ($_POST['addCredit'][$loopCount] != '') ? $_POST['addCredit'][$loopCount] : 0;
      $addVadhaaroGhataado = ($_POST['addVadhaaroGhataado'][$loopCount] != '') ? $_POST['addVadhaaroGhataado'][$loopCount] : 0;
      $qty              = ($_POST['qty'][$loopCount] != '') ? $_POST['qty'][$loopCount] : 0;
      $rate             = ($_POST['rate'][$loopCount] != '') ? $_POST['rate'][$loopCount] : 0;
      $crAmount         = ($_POST['crAmount'][$loopCount] != '') ? $_POST['crAmount'][$loopCount] : 0;
      $drAmount         = ($_POST['drAmount'][$loopCount] != '') ? $_POST['drAmount'][$loopCount] : 0;
      $vadhaaroGhataado = ($_POST['vadhaaroGhataado'][$loopCount] != '') ? $_POST['vadhaaroGhataado'][$loopCount] : 0;
  
      if($_POST['particulars'][$loopCount] != "" || $_POST['debit'][$loopCount] != "" || $_POST['credit'][$loopCount] != "" || $_POST['particular'][$loopCount] != "" ||
         $_POST['vadhaaroGhataado'][$loopCount] != "" || $_POST['addCredit'][$loopCount] != "" || $_POST['addDebit'][$loopCount] != "" || $_POST['addVadhaaroGhataado'][$loopCount] != "" || 
         $_POST['qty'][$loopCount] != "" || $_POST['rate'][$loopCount] != "" || $_POST['crAmount'][$loopCount] != "" || $_POST['drAmount'][$loopCount] != "" )
      { 
        $insertKaarigarDetail = "INSERT INTO kaarigardetail(kaarigarMasterId,particulars,debit,particular,credit,vadhaaroGhatado,addDebit,addCredit,addVadhaaroGhataado,qty,rate,crAmount,drAmount)
                                 VALUES (".$updatedKaarigarMstId.",'".$particulars."',".$debit.",'".$particular."',".$credit.",".$vadhaaroGhataado.",".$addDebit.",".$addCredit.",".$addVadhaaroGhataado.",".$qty.",".$rate.",
                                         ".$crAmount.",".$drAmount.")";
        $insertKaarigarDetailRes = mysql_query($insertKaarigarDetail);
      }
    $loopCount++;
    }
    
  }
  else
  {
    $insertMasterQuery = "INSERT INTO kaarigarmaster (kaarigarId,kaarigarDate,totalVadhGhat)
                          VALUES (".$_POST['kaarigarId'].",'".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',".$_POST['totalVadhGhat'].")";
    $insertMasterQueryRes = mysql_query($insertMasterQuery);
    $kaarigarMasterId = mysql_insert_id();
                          
    $loopCount1 = 0;
    while($loopCount1 < count($_POST['particulars']))
    { 
      $particulars      = ($_POST['particulars'][$loopCount1] != '') ? $_POST['particulars'][$loopCount1] : 0;
      $credit           = ($_POST['credit'][$loopCount1] != '') ? $_POST['credit'][$loopCount1] : 0;
      $particular       = ($_POST['particular'][$loopCount1] != '') ? $_POST['particular'][$loopCount1] : 0;
      $debit            = ($_POST['debit'][$loopCount1] != '') ? $_POST['debit'][$loopCount1] : 0;
      $addDebit         = ($_POST['addDebit'][$loopCount1] != '') ? $_POST['addDebit'][$loopCount1] : 0;
      $addCredit        = ($_POST['addCredit'][$loopCount1] != '') ? $_POST['addCredit'][$loopCount1] : 0;
      $addVadhaaroGhataado = ($_POST['addVadhaaroGhataado'][$loopCount1] != '') ? $_POST['addVadhaaroGhataado'][$loopCount1] : 0;
      $qty              = ($_POST['qty'][$loopCount1] != '') ? $_POST['qty'][$loopCount1] : 0;
      $rate             = ($_POST['rate'][$loopCount1] != '') ? $_POST['rate'][$loopCount1] : 0;
      $crAmount         = ($_POST['crAmount'][$loopCount1] != '') ? $_POST['crAmount'][$loopCount1] : 0;
      $drAmount         = ($_POST['drAmount'][$loopCount1] != '') ? $_POST['drAmount'][$loopCount1] : 0;
      $vadhaaroGhataado = ($_POST['vadhaaroGhataado'][$loopCount1] != '') ? $_POST['vadhaaroGhataado'][$loopCount1] : 0;
  
      if($_POST['particulars'][$loopCount1] != "" || $_POST['debit'][$loopCount1] != ''|| $_POST['credit'][$loopCount1] != '' || $_POST['particular'][$loopCount1] != '' ||
         $_POST['vadhaaroGhataado'][$loopCount1] != '' || $_POST['addCredit'][$loopCount1] != "" || $_POST['addDebit'][$loopCount1] != "" || $_POST['addVadhaaroGhataado'][$loopCount1] != "" || 
         $_POST['qty'][$loopCount1] != '' || $_POST['rate'][$loopCount1] != '' || $_POST['crAmount'][$loopCount1] != '' || $_POST['drAmount'][$loopCount1] != '' )
      { 
        $insertKaarigarDetail = "INSERT INTO kaarigardetail (kaarigarMasterId,particulars,debit,particular,credit,vadhaaroGhatado,addDebit,addCredit,addVadhaaroGhataado,qty,rate,crAmount,drAmount)
                                 VALUES ('".$kaarigarMasterId."','".$particulars."',".$debit.",'".$particular."',".$credit.",".$vadhaaroGhataado.",".$addDebit.",".$addCredit.",".$addVadhaaroGhataado.",".$qty.",".$rate.",".$crAmount.",
                                          ".$drAmount.")";
        $insertKaarigarDetailRes = mysql_query($insertKaarigarDetail);
      }
    $loopCount1++;
    }  
  }
  header("location:kaarigarDetail.php?kaarigarId=".$kaarigarId);
}

if(isset($_GET['kaarigarMasterId']) && ($_GET['kaarigarMasterId']) > 0)
{
   $selectKaarigar = "SELECT kaarigardetail.kaarigardetailId,kaarigardetail.kaarigarMasterId,kaarigardetail.particulars,kaarigardetail.particular,
                            kaarigardetail.debit,kaarigardetail.credit,kaarigardetail.vadhaaroGhatado,kaarigardetail.addDebit,kaarigardetail.addCredit,
                            kaarigardetail.addVadhaaroGhataado,kaarigardetail.qty,kaarigardetail.rate,kaarigardetail.crAmount,kaarigardetail.drAmount,
                            kaarigarmaster.kaarigarId,DATE_FORMAT(kaarigarmaster.kaarigarDate,'%Y-%m-%d') AS kaarigarMasterDate,kaarigarmaster.totalVadhGhat
                       FROM kaarigardetail
                       JOIN kaarigarmaster ON kaarigardetail.kaarigarMasterId = kaarigarmaster.kaarigarMasterId
                      WHERE kaarigarmaster.kaarigarMasterId = ".$_GET['kaarigarMasterId'];
  $selectKaarigarRes = mysql_query($selectKaarigar);
  $a = 0;
  while($kaarigarRow = mysql_fetch_assoc($selectKaarigarRes))
  {
    $kaarigarDetail[$a]['kaarigardetailId'] = $kaarigarRow['kaarigardetailId'];
    $kaarigarDetail[$a]['kaarigarMasterId'] = $kaarigarRow['kaarigarMasterId'];
    $kaarigarMasterId                       = $kaarigarRow['kaarigarMasterId'];
    $kaarigarDetail[$a]['kaarigarId']       = $kaarigarRow['kaarigarId'];
    $kaarigarDetail[$a]['particulars']      = $kaarigarRow['particulars'];
    $kaarigarDetail[$a]['particular']       = $kaarigarRow['particular'];
    $kaarigarDetail[$a]['debit']            = $kaarigarRow['debit'];
    $kaarigarDetail[$a]['credit']           = $kaarigarRow['credit'];
    $kaarigarDetail[$a]['vadhaaroGhatado']  = $kaarigarRow['vadhaaroGhatado'];
    $kaarigarDetail[$a]['addDebit']         = $kaarigarRow['addDebit'];
    $kaarigarDetail[$a]['addCredit']        = $kaarigarRow['addCredit'];
    $kaarigarDetail[$a]['addVadhaaroGhataado']  = $kaarigarRow['addVadhaaroGhataado'];
    $kaarigarDetail[$a]['qty']              = $kaarigarRow['qty'];
    $kaarigarDetail[$a]['rate']             = $kaarigarRow['rate'];
    $kaarigarDetail[$a]['crAmount']         = $kaarigarRow['crAmount'];
    $kaarigarDetail[$a]['drAmount']         = $kaarigarRow['drAmount'];
    $kaarigarDetail[$a]['kaarigarDate']     = $kaarigarRow['kaarigarMasterDate'];
    $kaarigarDetail[$a]['totalVadhGhat']    = $kaarigarRow['totalVadhGhat'];
    $totVadhGhat += $kaarigarRow['vadhaaroGhatado'];
    $a++;
  }
}

$selectKaarigar = "SELECT kaarigarId,kaarigarName 
                     FROM kaarigar
                    ORDER BY kaarigarName DESC";
$selectKaarigarRes = mysql_query($selectKaarigar);
$a = 0;
$kaarigar['id'][$a] = 0;
$kaarigar['name'][$a] = "Select";
$a++;  
while($kaarigarRow = mysql_fetch_assoc($selectKaarigarRes))
{
  $kaarigar['id'][$a] = $kaarigarRow['kaarigarId'];
  $kaarigar['name'][$a] = $kaarigarRow['kaarigarName'];
  $a++;  
}

$smarty = new Smarty;
require("bottom.php");
$smarty->assign("l_kaarigarName",$l_kaarigarName);
$smarty->assign("l_particulars",$l_particulars);
$smarty->assign("l_credit",$l_credit);
$smarty->assign("l_debit",$l_debit);
$smarty->assign("l_vadhaaroGhataado",$l_vadhaaroGhataado);
$smarty->assign("l_qty",$l_qty);
$smarty->assign("l_rate",$l_rate);
$smarty->assign("l_amount",$l_amount);
$smarty->assign("l_add",$l_add);
$smarty->assign("l_date",$l_date);
$smarty->assign("l_kaarigarEntry",$l_kaarigarEntry);
$smarty->assign("l_remove",$l_remove);
$smarty->assign("l_crAmount",$l_crAmount);
$smarty->assign("l_drAmount",$l_drAmount);
$smarty->assign("l_save",$l_save);
$smarty->assign("l_total",$l_total);

$smarty->assign("kaarigar",$kaarigar);
$smarty->assign("kaarigarDetail",$kaarigarDetail);
$smarty->assign("kaarigarMasterId",$kaarigarMasterId);
$smarty->assign("updatedKaarigarMstId",$updatedKaarigarMstId);
$smarty->assign("totVadhGhat",$totVadhGhat);
$smarty->display("kaarigarEntry.tpl");
?>